var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sid: '',
        userInfo: {},
        info: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this

        that.setData({
            sid: options.sid
        })

        //用户授权登录
        app.login(function (userInfo) {
            console.log(userInfo);
            that.setData({
                userInfo: userInfo
            });

            that.getCouponInfo();
        });
    },

    getCouponInfo() {
        var that = this;
        var paraArr = new Array();
        paraArr['id'] = that.data.sid;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/coupon/couponInfo',
            data: {
                id: paraArr['id'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                console.log(res.data);
                res.data.data._cut_price = Number(res.data.data.cut_price)
                res.data.data._min_price = Number(res.data.data.min_price)
                that.setData({
                    info: res.data.data
                })
            }
        })
    },

    // 领取按钮
    receiveBtn() {
        var that = this;
        var paraArr = new Array();
        paraArr['id'] = that.data.info.id;
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/coupon/couponReceive',
            data: {
                id: paraArr['id'],
                user_id: paraArr['user_id'],
                sign: sign
            },
            method: 'POST',
            header: {},
            success: function (res) {
                if (res.data.code == 1001) {
                    wx.showToast({
                        title: '领取成功',
                        icon: 'none',
                        duration: 2000,
                    })

                    setTimeout(function () {
                        app.redirect('user/index');
                    }, 2000);
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })

                }
            }
        })
    },

    // 领取提示按钮
    receiveTipBtn() {
        wx.showToast({
            title: '该优惠券已被领取',
            icon: 'none'
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCouponInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})